package migrate

import (
	"flag"
	"fmt"
	"github.com/urfave/cli"
	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/logc"
	"go-zero-demo/app/datacenter/cron/internal/cmd"
	"go-zero-demo/app/datacenter/cron/internal/config"
	"go-zero-demo/app/datacenter/cron/internal/svc"
	"go-zero-demo/common/model"
)

var Migrate = cli.Command{
	Name:   "migrate",
	Usage:  "初始任务",
	Action: runMigrate,
	Flags: []cli.Flag{
		cmd.StringFlag("config, c", "etc/cron.yaml", "Custom configuration file path"),
	},
}

func runMigrate(cliCtx *cli.Context) error {
	// 初始化配置文件，实例化service
	flag.Parse()
	//var configFile = flag.String("f", "etc/cron-api.yaml", "the config file")
	configFile := cliCtx.String("config")
	var c config.Config
	conf.MustLoad(configFile, &c)
	logc.MustSetup(c.Log)
	svcCtx := svc.NewMigrateSvc(c)

	err := model.AutoMigrate(svcCtx.DB)
	if err != nil {
		fmt.Println(err)
	}

	return nil
}
